﻿package com.sz.courseserial.bmo.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.json.JSONSerializer;

import com.sz.courseserial.bmo.ICourseSerialBMO;
import com.sz.courseserial.dao.ICourseSerialDAO;

public class CourseSerialBMO implements ICourseSerialBMO{
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	public ICourseSerialDAO getCourseSerialDAO() {
		return courseSerialDAO;
	}

	public void setCourseSerialDAO(ICourseSerialDAO courseSerialDAO) {
		this.courseSerialDAO = courseSerialDAO;
	}

	protected ICourseSerialDAO courseSerialDAO = null;
	
	@SuppressWarnings("unchecked")
	public long addCourseSerial(Map map) throws Exception {
		return courseSerialDAO.addCourseSerial(map);
	}
	
	@SuppressWarnings("unchecked")
	/**
	 * 可接受的条件：
	 * TICKET_ID STATUS CUST_TEL CUST_NAME BUSINESS_ID REGION CREATE_MANAGER LOCK_MANAGER
	 */
	public List queryCourseSerial(Map map) throws Exception {
		return courseSerialDAO.queryCourseSerial(map);
	}
	
	@SuppressWarnings("unchecked")
	public long queryCourseSerialCount(Map map) throws Exception {
		return courseSerialDAO.queryCourseSerialCount(map);
	}
	
	public List queryCourseSerial2(Map map) throws Exception {
		return courseSerialDAO.queryCourseSerial2(map);
	}
	
	@SuppressWarnings("unchecked")
	public long queryCourseSerialCount2(Map map) throws Exception {
		return courseSerialDAO.queryCourseSerialCount2(map);
	}
	
	@SuppressWarnings("unchecked")
	public List queryCourseSerialPage(Map map) throws Exception {
		return courseSerialDAO.queryCourseSerialPage(map);
	}
	
	@SuppressWarnings("unchecked")
	public List queryCourseSerialPage2(Map map) throws Exception {
		return courseSerialDAO.queryCourseSerialPage2(map);
	}
	
	@SuppressWarnings("unchecked")
	public String lockTickets(List list) throws Exception {
		if(list == null) return "参数错误，或参数转换错误";
		
		int nSuccess = 0;
		int nFail = 0;
		StringBuffer err = new StringBuffer();
		
		for(int n = 0; n < list.size(); ++n) {
			Map map = (Map)list.get(n);
			String msg = courseSerialDAO.lockTicket(map);
			if(msg != null) {
				err.append(msg);
				err.append("\n");
				++nFail;
			} else {
				++nSuccess;
			}
		}

		if(nFail == 0) {			
			return String.format("成功处理%s条订单", nSuccess);
		} else {
			return String.format("成功处理%s条订单，失败%s条\n详细信息:\n%s", nSuccess, nFail, err.toString());
		}
	}
	
	@SuppressWarnings("unchecked")
	public String cancelTicket(Map map) throws Exception {
		String msg = courseSerialDAO.cancelTicket(map);
		//if(msg == null) msg = "操作成功"; //ajax的问题 先屏蔽
		return msg;
	}
	
	@SuppressWarnings("unchecked")
	public String editCourseSerial(Map map) throws Exception {
		String msg = courseSerialDAO.editCourseSerial(map);		
		//if(msg == null) msg = "操作成功";  //ajax的问题 先屏蔽
		return msg;
	}
	
	@SuppressWarnings("unchecked")
	public String editCourseSerial2(Map map) throws Exception {
		String msg = courseSerialDAO.editCourseSerial2(map);		
		//if(msg == null) msg = "操作成功";  //ajax的问题 先屏蔽
		return msg;
	}
	
	@SuppressWarnings("unchecked")
	public String delCourseSerial(Map map) throws Exception {
		String msg = courseSerialDAO.delCourseSerial(map);		
		//if(msg == null) msg = "操作成功";  //ajax的问题 先屏蔽
		return msg;
	}
	
	@SuppressWarnings("unchecked")
	public String delCourseSerial2(Map map) throws Exception {
		String msg = courseSerialDAO.delCourseSerial2(map);		
		//if(msg == null) msg = "操作成功";  //ajax的问题 先屏蔽
		return msg;
	}
	
	@SuppressWarnings("unchecked")
	public String nextStatus(Map map) throws Exception {
		String msg = courseSerialDAO.nextStatus(map);
		//if(msg == null) msg = "操作成功";  //ajax的问题 先屏蔽
		return msg;
	}
	
	@SuppressWarnings("unchecked")
	public String confirmfailedTicket(Map map) throws Exception {
		String msg = courseSerialDAO.confirmfailedTicket(map);
		//if(msg == null) msg = "操作成功"; //ajax的问题 先屏蔽
		return msg;
	}
	
	@SuppressWarnings("unchecked")
	public List queryBusinessTickets(Map map) throws Exception {
		return courseSerialDAO.queryBusinessTickets(map);
	}
	
	@SuppressWarnings("unchecked")
	public long queryBusinessTicketsCount(Map map) throws Exception {
		return courseSerialDAO.queryBusinessTicketsCount(map);
	}
	
	@SuppressWarnings("unchecked")
	public List queryBusinessTicketsPage(Map map) throws Exception {
		return courseSerialDAO.queryBusinessTicketsPage(map);
		
	}
	
	public void checkTicket_Business(Map map) throws Exception {
		courseSerialDAO.checkTicket_Business(map);
	}
	
	public void checkTicket_Custom(Map map) throws Exception {
		courseSerialDAO.checkTicket_Custom(map);
	}

}
